﻿
Imports System.IO
Imports System.Reflection
Imports System.Text

Public Class UtilsHelper
    ''' <summary>
    ''' 将c# DateTime时间格式转换为Unix时间戳格式  
    ''' </summary>
    ''' <returns></returns>
    Public Shared Function ConvertDateTimeToInt() As Long
        Dim CurTime As Date = Now
        Dim startTime As Date = TimeZone.CurrentTimeZone.ToLocalTime(New Date(1970, 1, 1, 0, 0, 0, 0))
        Return (CurTime.Ticks - startTime.Ticks) \ 10000 '除10000调整为13位
    End Function

    ''' <summary>        
    ''' 时间戳转为C#格式时间        
    ''' </summary>        
    ''' <param name="timeStamp"></param>        
    ''' <returns></returns>        
    Private Shared Function ConvertStringToDateTime(ByVal timeStamp As String) As Date
        Dim dtStart As Date = TimeZone.CurrentTimeZone.ToLocalTime(New Date(1970, 1, 1))
        Dim lTime As Long = Long.Parse(timeStamp & "0000")
        Dim toNow As New TimeSpan(lTime)
        Return dtStart.Add(toNow)
    End Function

    ''' <summary>
    ''' 将listview中的数据导出到DataTable中
    ''' </summary>
    ''' <param name="lv"></param>
    ''' <param name="dt"></param>
    Public Shared Sub listViewToDataTable(ByVal lv As ListView, ByVal dt As DataTable)
        Dim dr As DataRow
        dt.Clear()
        dt.Columns.Clear()
        For k As Integer = 0 To lv.Columns.Count - 1
            dt.Columns.Add(lv.Columns(k).Text.Trim().ToString()) '生成DataTable列头
        Next k
        For i As Integer = 0 To lv.Items.Count - 1
            dr = dt.NewRow()
            For j As Integer = 0 To lv.Columns.Count - 1
                dr(j) = lv.Items(i).SubItems(j).Text.Trim()
            Next j
            dt.Rows.Add(dr) '每行内容
        Next i
    End Sub

    ''' <summary>
    ''' 将dataTable中的数据导出到txt文本中
    ''' </summary>
    ''' <param name="dt"></param>
    ''' <param name="fileName"></param>
    Public Shared Sub DataTableToTxt(ByVal dt As DataTable, ByVal fileName As String)
        Dim builder As New StringBuilder()
        Dim lineStrs As New List(Of String)
        '加标题
        For i As Integer = 0 To dt.Columns.Count - 1
            lineStrs.Add(dt.Columns(i).Caption.ToString())
        Next i
        builder.AppendLine(String.Join(",", lineStrs.ToArray))
        '加内容
        For i As Integer = 0 To dt.Rows.Count - 1
            Dim row As New List(Of String)
            For j As Integer = 0 To dt.Columns.Count - 1
                row.Add(dt.Rows(i)(j).ToString())
            Next j
            builder.AppendLine(String.Join(",", row.ToArray))
        Next i
        '写入文本
        Using file As New System.IO.FileStream(fileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)
            Using text As System.IO.TextWriter = New System.IO.StreamWriter(file, System.Text.Encoding.Default)
                text.Write(builder.ToString())
            End Using
        End Using
    End Sub
    ''' <summary>
    ''' 将datatable数据导出为csv文件
    ''' </summary>
    ''' <param name="dt"></param>
    ''' <param name="fullPath"></param>
    Public Shared Sub DataTableToCSV(ByVal dt As DataTable, ByVal fullPath As String)
        Dim fs = New FileStream(fullPath, FileMode.Create, FileAccess.Write)
        Dim sw = New StreamWriter(fs, Encoding.UTF8)
        Dim data = ""
        '写出列名称
        For i = 0 To dt.Columns.Count - 1
            data &= dt.Columns(i).ColumnName
            If i < dt.Columns.Count - 1 Then
                data &= ","
            End If
        Next i
        sw.WriteLine(data)
        '写出各行数据
        For i = 0 To dt.Rows.Count - 1
            data = ""
            For j = 0 To dt.Columns.Count - 1
                Dim str = dt.Rows(i)(j).ToString().Trim
                str = str.Replace("""", """""") '替换英文冒号 英文冒号需要换成两个冒号
                If str.Contains(",") OrElse str.Contains("""") OrElse str.Contains(ControlChars.Cr) OrElse str.Contains(ControlChars.Lf) Then '含逗号 冒号 换行符的需要放到引号中
                    str = String.Format("""{0}""", str)
                End If
                data &= str
                If j < dt.Columns.Count - 1 Then
                    data &= ","
                End If
            Next j
            sw.WriteLine(data)
        Next i
        sw.Close()
        fs.Close()
    End Sub

End Class
